package com.qyp.core;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTree;

public class TreeDemo extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TreeDemo frame = new TreeDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TreeDemo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);

		// 创建没有父节点和子节点、但允许有子节点的树节点，并使用指定的用户对象对它进行初始化。
		// public DefaultMutableTreeNode(Object userObject)
		DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部");
		node1.add(new DefaultMutableTreeNode(new User("小花", "女", 21)));
		node1.add(new DefaultMutableTreeNode(new User("小虎", "男", 22)));
		node1.add(new DefaultMutableTreeNode(new User("小龙", "男", 12)));

		DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部");
		node2.add(new DefaultMutableTreeNode(new User("小叶", "女", 34)));
		node2.add(new DefaultMutableTreeNode(new User("小雯", "女", 23)));
		node2.add(new DefaultMutableTreeNode(new User("小夏", "男", 22)));

		DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理");

		top.add(new DefaultMutableTreeNode(new User("总经理", "男", 34)));
		top.add(node1);
		top.add(node2);

		JTree tree = new JTree(top);
		tree.setBounds(6, 6, 128, 260);
		contentPane.add(tree);
	}

}
